#include<at89c51ed2.h>
#include<stdio.h>
#include<mcs51reg.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include "io.h"


void lcdData(char val,int reg);
int lcdDataRead();






void lcddelay(int time){
	int i;
	for(i=0;i<time+1;i++){
	}
}	
	
void checkBusyFlag()
{
	volatile int val;

	val=lcdDataRead();
	val&=0x80;
	while(val!=0x00)
	{
		val=lcdDataRead();
		val&=0x80;
		printf("LCD Busy \n\r");
	}


}

/******************************************************************************************/
/*************************************************************************************************************************************/
// Name: lcdinit()
// Description: Initializes the LCD (see Figure 25 on page 212
//  of the HD44780U data sheet).
void lcdinit(){ 

	/*Unlock Commands*/
	lcddelay(16000);
	
	lcdData(0x30,0);
		
	/*wait for more than 4.1 ms*/
	
	lcddelay(6000);
	
	lcdData(0x30,0);
	
	/*wait for 100 us*/
	
	lcddelay(6000);
	
	lcdData(0x30,0);
	
	//printf("3\n\r");
	
	/*Function Set Command*/
	//lcddelay(); /*Poll for BF=0*/
	lcddelay(500);
	
	lcdData(0x38,0);
	
	//printf("4\n\r");
	/*turn display OFF*/
	//lcddelay(); /*Poll for BF=0*/
	lcddelay(500);
	
	lcdData(0x08,0);
	//printf("5\n\r");
	/*turn display ON*/
	//lcddelay(); /*Poll for BF=0*/
	lcddelay(500);
	
	lcdData(0x0F,0); /*c*/
	
	/*Entry Mode Set Command*/
	//lcddelay(); /*Poll for BF=0*/
	lcddelay(500);
	
	lcdData(0x06,0);
	
	
	/*clear screen and send cursor home*/
	//lcddelay(); /*Poll for BF=0*/
	lcddelay(500);
	
	lcdData(0x01,0);
	lcddelay(500);
	
}
/**************************************************************/






/*set DDRAM Address*/
void lcdgotoaddr(unsigned char addr){

	 
	addr|=0x80;
	
	//checkBusyFlag(); /*Poll for BF=0*/
	
	lcdData(addr,0);	
	
}


void decPoint_hexPoint(int val)
{
	//char hex;
	printf("im here");
	printf("Value is %x",val);

	if(val<=15){
	val=val+0;   //1st line
	}
	if(val>=16&&val<=31){
	val=val+48; //HEX 30  //2nd line
	}
	if(val>=32&&val<=47){
	val=val-16; //HEX 10  //3rd line
	}
	if(val>=48&&val<=63){
	val=val+32; //HEX 20 //4th line
	}
	
	printf("Address::%d",val);
	lcdgotoaddr(val);
}

/**********************************************************************************************/



void lcdgotoloc(int x,int y)
{
	

if(y==0){
	decPoint_hexPoint(x);
	
	}
if(y==1){
	decPoint_hexPoint(16+x);
	}
if(y==2){
	decPoint_hexPoint(32+x);
	}
if(y==3){
	decPoint_hexPoint(48+x);
	}

}


/**************************************************************************************/
/************************************************/
void lcdputch(char cc)
{
	
	//lcddelay(500); /*Poll for BF=0*/
	checkBusyFlag(); /*Poll for BF=0*/

	lcdData(cc,1);
}
/**********************************************/

void lcdputstr(char *val){ //put characters in a string
	int i;
	int x_Counter;
	
	int y;
	int currentAddr;
	
	currentAddr=lcdDataRead();
	currentAddr&=0x7F;
	printf("current addr value is %x",currentAddr);
	//currentAddr=currentAddr-1;
	
	if(currentAddr<=0x0F){
		y=0;
		
	}
	else if(currentAddr>=0x40&&currentAddr<=0x4F){
		y=1;
		currentAddr=currentAddr-0x40;
	
	}
	else if(currentAddr>=0x10&&currentAddr<=0x1F){
		y=2;
		currentAddr=currentAddr-0x10;
	}
	else if(currentAddr>=0x50&&currentAddr<=0x5F){
		y=3;
		currentAddr=currentAddr-0x50;
	}
	
	//lcdgotoloc(1,15);
	
	x_Counter=16-currentAddr;	



		for(i=0;i<=(strlen(val)-1);i++){
		
			lcdputch(val[i]);
			
			
			x_Counter--;
			printf("X Counter%d\n\r",x_Counter);
			
			if(y==3&&x_Counter==0){
				y=0;
				lcdgotoloc(0,y);
				x_Counter=16;
			}
			
			if(x_Counter==0){
				
				y=y+1;
				lcdgotoloc(0,y);
				x_Counter=16;
			}
			
		}			
		
	
}	
				
		
void lcdputstring(char *val,int x,int y){ //put characters in a string
	int i;
	int x_Counter;
	
	//lcdgotoloc(x,y);
	
	x_Counter=16-x;	
	


		for(i=0;i<=(strlen(val)-1);i++){
		
			lcdputch(val[i]);
			
			
			x_Counter--;
		
			
			if(y==3&&x_Counter==0){
				y=0;
				lcdgotoloc(0,y);
				x_Counter=16;
			}
			
			if(x_Counter==0){
				
				y=y+1;
				lcdgotoloc(0,y);
				x_Counter=16;
			}
			
		}			
		
	
}	



/************************************************************************************************/
int lcd_asciiInput(char * val){
int num[3];
char * hex;
int ans;
int i;
int mul;

i=0;
hex=(char *)malloc(50);


while(i<strlen(val))
	{
	
	if(val[i]=='A'||val[i]=='a'){
		num[i]=10;
	}
	
	else if(val[i]=='B'||val[i]=='b'){
		num[i]=11;
	}
	else if(val[i]=='C'||val[i]=='c'){
		num[i]=12;
	}
	else if(val[i]=='D'||val[i]=='d'){
		num[i]=13;
	}
	else if(val[i]=='E'||val[i]=='e'){
		num[i]=14;
	}
	else if(val[i]=='F'||val[i]=='f'){
		num[i]=15;
	}
	else{
		num[i]=val[i]-48;
		
	}
	i++;
	}
i=strlen(val)-1;
mul=1;
ans=0;

while(i>=0){
	
ans=ans+mul*num[i];
mul=mul*16;
i--;
}

printf("ascii value:%d",ans);
return ans;
}



int lcd_singleCharconv(char val){
int num;
	
	if(val=='A'||val=='a'){
		num=10;
	}
	
	else if(val=='B'||val=='b'){
		num=11;
	}
	else if(val=='C'||val=='c'){
		num=12;
	}
	else if(val=='D'||val=='d'){
		num=13;
	}
	else if(val=='E'||val=='e'){
		num=14;
	}
	else if(val=='F'||val=='f'){
		num=15;
	}
	else{
		num=val-48;
		
	}

return num;
}

void lcdgotoxy(unsigned char row,unsigned char column){
	int x;
	int y;
	
	x=lcd_singleCharconv(column);
	y=lcd_singleCharconv(row);
	
	lcdgotoloc(x,y);

}
